#!/bin/bash
#
#********************************************************************
# Author: zhangjunjie
# Date: 2022-05-14
# FileName： centos_install_mysql.sh
#********************************************************************
# 社区版下载网址  https://dev.mysql.com/downloads/mysql/
# MySQL Download URL: https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
#
# yum 源地址信息 更多版本信息, 可以去 https://mirrors.tuna.tsinghua.edu.cn/mysql/ 查看
# 	mysql   ==> 可以在该地址选择 https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/
# 		centos 7 MySQL 5.7  	yum 源 https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
# 		centos 7 MySQL 8.0  	yum 源 https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/
#	mariadb ==> 可以在该地址选择 https://mariadb.org/download/?t=repo-config, 然后复制下面的仓库地址
# 		centos 7 Mariadb 10.5   yum 源 https://mirrors.aliyun.com/mariadb/yum/10.5/centos7-amd64
# 二进制安装包地址
# 	mysql
#		centos 7 MySQL 5.6.49	https://mirrors.aliyun.com/mysql/MySQL-5.6/mysql-5.6.49-linux-glibc2.12-x86_64.tar.gz
#		centos 7 MySQL 5.7.36	https://mirrors.aliyun.com/mysql/MySQL-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
#		centos 7 MySQL 8.0.27	https://mirrors.aliyun.com/mysql/MySQL-8.0/mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
# 源码包
#	mysql
#		centos 7 MySQL 5.6.51 	https://mirrors.aliyun.com/mysql/MySQL-5.6/mysql-5.6.51.tar.gz
#	mariadb
#		centos 7 mariadb 10.2.44 https://mirrors.aliyun.com/mariadb/mariadb-10.2.44/source/mariadb-10.2.44.tar.gz


## 判断自定义方法
if [ ! -e self_function.sh ]; then
	rpm -q wget || (yum install wget -y || { echo -e "\E[31;1m 安装 wget 软件包失败 \E[0m" ; exit; })

	echo -e "\E[33;1m 下载自定义方法中 \E[0m"
	wget https://gitee.com/gkdaxue/shell_script/raw/master/self_function.sh
	if [ ! -e self_function.sh ]; then 
		echo -e "\E[31;1m 下载自定义方法失败 \E[0m"
		exit;
	fi
fi

## 加载自定义方法
. ./self_function.sh
actions 'echo_tips' "加载自定义方法";


## 重置的 ROOT 账户密码, 需要设置复杂点
MYSQL_ROOT_PASSWORD='Root#123.'

### ----------- 源码安装常量的位置 ----------- ###
SRC_DIR='/usr/local/src'
### ----------- 源码安装常量的位置 ----------- ###


## 检查安装的必要条件
check (){
	## 检查是不是管理员
	if [ $UID -ne 0 ]; then
		actions 'echo_error' "当前用户不是管理员，安装失败"
		exit;
	fi

	id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; actions 'echo_tips' '创建 mysql 用户'; }

	if [ -e /usr/loca/mysql ]; then
		actions 'echo_error' "MySQL 已经安装, 请检查"
		exit;
	fi
	
	if [ ! -e $SRC_DIR/$MYSQL_FILE ]; then
		actions 'echo_tips' "MySQL 源码文件不存在， 正在下载中， 请稍后"
		wget $MYSQL_DOWNLOAD_URL -O $SRC_DIR/$MYSQL_FILE
		[ ! -e  $SRC_DIR/$MYSQL_FILE ] || { actions 'echo_error' '文件下载失败, 请稍后重试'; exit; }
	elif [ -d /usr/local/mysql ]; then
		actions 'echo_error' "/usr/local/mysql 目录已经存在, 请确认数据库是否安装, 即将退出"
		exit;
	elif [ -e $PREFIX ]; then
		actions 'echo_error' "安装目录已经存在 $PREFIX , 请确认数据库是否安装, 即将退出"
		exit;
	fi
}

install_menu=(
"centos_7_yum_install_mysql_5_6_51"
"centos_7_yum_install_mysql_5_7"
"centos_7_yum_install_mysql_8_0_laste"
"centos_7_yum_install_mysql_8_0_21"
"centos_7_yum_install_mariadb_10_5"
"centos_7_binary_install_mysql_5_6_49"
"centos_7_binary_install_mysql_5_7_36"
"centos_7_binary_install_mysql_8_0_27"
"centos_7_source_code_install_mysql_5_6_51"
"centos_7_source_code_install_mariadb_10_2_44"
"centos_7_backup_one_database_mariadb_10_5"
"centos_7_backup_all_database_mariadb_10_5"
)
PS3="请选择你要如何安装 : "
echo "我们提供如下菜单: "
select select_menu in ${install_menu[@]} ; do
	case $select_menu in
		## centos 7 yum源安装 MySQL 5.6.51 中最新的版本
		"centos_7_yum_install_mysql_5_6_51")
			wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
			rpm -ivh mysql-community-release-el7-5.noarch.rpm
			yum install mysql-server
			systemctl enable --now mysqld
			systemctl status mysqld
			exit;
			;;
		## centos 7 yum源安装 MySQL 5.7 中最新的版本
		"centos_7_yum_install_mysql_5_7")
			cat > /etc/yum.repos.d/mysql_yum_5_7.repo <<EOF
[mysql_yum_5_7]
name=mysql_yum_5_7
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/
gpgcheck=0
enabled=1
EOF
			yum clean all && yum makecache
			yum -y install mysql-community-server
			systemctl enable --now mysqld

			## root 账户的密码是生成的一个临时的密码, 所有有两种方式可以重新重置密码
			## 我们就使用最简单的 命令行模式来重置 root 密码吧
			## 方式1 :
			## 		[~]# mysql -uroot -p"`awk '/A temporary password/{print $NF}' /var/log/mysqld.log`"
			##		mysql> alter user root@'localhost' identified by 'NewPassWord';
			## 		mysql> exit
			##		[~]# systemctl restart mysql
			## 方式2 :
			## 		[~]# mysqladmin -uroot -p"`awk '/A temporary password/{print $NF}' /var/log/mysqld.log`" password 'NewPassWord'
			##		[~]# systemctl restart mysql
			actions 'echo_success' "开始重新设置 root 密码"
			MYSQL_TEMPORARY_PASSWORD=`awk '/A temporary password/{print $NF}' /var/log/mysqld.log`
			mysqladmin -uroot -p$MYSQL_TEMPORARY_PASSWORD password $MYSQL_ROOT_PASSWORD

			systemctl restart mysqld
			actions 'echo_success' "数据库安装完成"
			actions 'echo_success' "启动信息如下"
			systemctl status mysqld
			exit;
			;;
		## centos 7 yum源安装 MySQL 8.0 中最新的版本
		"centos_7_yum_install_mysql_8_0_laste")
			cat > /etc/yum.repos.d/mysql_yum_8_0.repo <<EOF
[mysql_yum_8_0]
name=mysql_yum_8_0
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/
gpgcheck=0
enabled=1
EOF
			yum clean all && yum makecache
			yum -y install mysql-community-server
			systemctl enable --now mysqld

			## root 账户的密码是生成的一个临时的密码, 所有有两种方式可以重新重置密码
			## 我们就使用最简单的 命令行模式来重置 root 密码吧
			## 方式1 :
			## 		[~]# mysql -uroot -p"`awk '/A temporary password/{print $NF}' /var/log/mysqld.log`"
			##		mysql> alter user root@'localhost' identified by 'NewPassWord';
			## 		mysql> exit
			##		[~]# systemctl restart mysql
			## 方式2 :
			## 		[~]# mysqladmin -uroot -p"`awk '/A temporary password/{print $NF}' /var/log/mysqld.log`" password 'NewPassWord'
			##		[~]# systemctl restart mysql
			actions 'echo_success' "开始重新设置 root 密码"
			MYSQL_TEMPORARY_PASSWORD=`awk '/A temporary password/{print $NF}' /var/log/mysqld.log`
			mysqladmin -uroot -p$MYSQL_TEMPORARY_PASSWORD password $MYSQL_ROOT_PASSWORD

			systemctl restart mysqld
			actions 'echo_success' "数据库安装完成"
			actions 'echo_success' "启动信息如下"
			systemctl status mysqld
			exit;
			;;
			## centos 7 yum源安装 MySQL 8.0_21
		"centos_7_yum_install_mysql_8_0_21")
			cat > /etc/yum.repos.d/mysql_yum_8_0_21.repo <<EOF
[mysql_yum_8_0_21]
name=mysql_yum_8_0_21
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/
gpgcheck=0
enabled=1
EOF
			yum clean all && yum makecache
			yum -y install mysql-community-server-8.0.21
			systemctl enable --now mysqld

			## root 账户的密码是生成的一个临时的密码, 所有有两种方式可以重新重置密码
			## 我们就使用最简单的 命令行模式来重置 root 密码吧
			## 方式1 :
			## 		[~]# mysql -uroot -p"`awk '/A temporary password/{print $NF}' /var/log/mysqld.log`"
			##		mysql> alter user root@'localhost' identified by 'NewPassWord';
			## 		mysql> exit
			##		[~]# systemctl restart mysql
			## 方式2 :
			## 		[~]# mysqladmin -uroot -p"`awk '/A temporary password/{print $NF}' /var/log/mysqld.log`" password 'NewPassWord'
			##		[~]# systemctl restart mysql
			actions 'echo_success' "开始重新设置 root 密码"
			MYSQL_TEMPORARY_PASSWORD=`awk '/A temporary password/{print $NF}' /var/log/mysqld.log`
			mysqladmin -uroot -p$MYSQL_TEMPORARY_PASSWORD password $MYSQL_ROOT_PASSWORD

			systemctl restart mysqld
			actions 'echo_success' "数据库安装完成"
			actions 'echo_success' "启动信息如下"
			systemctl status mysqld
			exit;
			;;			
		## centos 7 yum 源安装 Mariadb 10.5 中最新的版本
		"centos_7_yum_install_mariadb_10_5")
			cat > /etc/yum.repos.d/mariadb_yum_10_5.repo <<EOF
[mariadb_yum_10_5]
name=mariadb_yum_10_5
baseurl=https://mirrors.aliyun.com/mariadb/yum/10.5/centos7-amd64
enabled=1
gpgcheck=0
EOF
			yum clean all && yum makecache
			yum -y install mariadb-server
			systemctl enable --now mariadb

			## 设置 root 账户的密码, 默认为空
			## [~]# mysql -uroot
			## mysql> alter user root@'localhost' identified by 'NewPassWord';
			## mysql> exit
			## [~]# systemctl restart mariadb
			actions 'echo_success' "数据库安装完成"
			systemctl status mariadb
			exit;
			;;
		## centos 7 二进制安装 MySQL 5.6.49 版本
		## 也需要进行安装加固的操作 mysql_secure_installation
		"centos_7_binary_install_mysql_5_6_49")
			SRC_DIR='/usr/local/src'
			MYSQL_VERSION='5.6.49'
			MYSQL_FILE="mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64.tar.gz"
			MYSQL_DIR="mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64"
			PREFIX="/apps/mysql-`echo ${MYSQL_VERSION} | tr . _`"
			DATA_DIR="/data/mysql-`echo ${MYSQL_VERSION} | tr . _`"
			MYSQL_DOWNLOAD_URL="https://mirrors.aliyun.com/mysql/MySQL-5.6/${MYSQL_FILE}"

			yum -y install libaio numactl-libs perl-Data-Dumper autoconf
			if [ $? -ne 0 ]; then
				actions 'echo_error' "编译工具安装失败, 请检查"
				exit;
			fi

			cd $SRC_DIR
			check
			mkdir -p $PREFIX ${DATA_DIR}
			tar xvf $MYSQL_FILE
			mv $MYSQL_DIR/* $PREFIX
			ln -sv $PREFIX /usr/local/mysql
			chown -R mysql.mysql $PREFIX
			chown -R mysql.mysql $DATA_DIR
			echo 'PATH='$PREFIX'/bin:$PATH' > /etc/profile.d/mysql.sh
			. /etc/profile.d/mysql.sh

			$PREFIX/scripts/mysql_install_db --user=mysql --datadir=${DATA_DIR} --basedir=$PREFIX
			cat > /etc/my.cnf <<EOF
[mysqld]
server-id=`hostname -I|cut -d. -f4`
datadir=${DATA_DIR}
socket=${DATA_DIR}/mysql.sock

[client]
socket=${DATA_DIR}/mysql.sock

[mysqld_safe]
log-error=${DATA_DIR}/mysql.log
pid-file=${DATA_DIR}/mysql.pid
EOF
			cp ${PREFIX}/support-files/mysql.server /etc/init.d/mysqld
			chmod +x /etc/init.d/mysqld
			chkconfig --add mysqld
			chkconfig mysqld on
			systemctl restart mysqld
			actions 'echo_success' "数据库安装完成"
			exit;
			;;	
		## centos 7 二进制安装 MySQL 5.7.36 版本
		## 也需要进行安装加固的操作 mysql_secure_installation
		"centos_7_binary_install_mysql_5_7_36")
			SRC_DIR='/usr/local/src'
			MYSQL_VERSION='5.7.36'
			MYSQL_FILE="mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64.tar.gz"
			MYSQL_DIR="mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64"
			PREFIX="/apps/mysql-`echo ${MYSQL_VERSION} | tr . _`"
			DATA_DIR="/data/mysql-`echo ${MYSQL_VERSION} | tr . _`"
			MYSQL_DOWNLOAD_URL="https://mirrors.aliyun.com/mysql/MySQL-5.7/${MYSQL_FILE}"

			yum -y install libaio numactl-libs perl-Data-Dumper autoconf
			if [ $? -ne 0 ]; then
				actions 'echo_error' "编译工具安装失败, 请检查"
				exit;
			fi

			cd $SRC_DIR
			check
			mkdir -p $PREFIX ${DATA_DIR}
			tar xvf $MYSQL_FILE
			mv $MYSQL_DIR/* $PREFIX
			ln -sv $PREFIX /usr/local/mysql
			chown -R mysql.mysql $PREFIX
			chown -R mysql.mysql $DATA_DIR
			echo 'PATH='$PREFIX'/bin:$PATH' > /etc/profile.d/mysql.sh
			. /etc/profile.d/mysql.sh

			cat > /etc/my.cnf <<EOF
[mysqld]
server-id=`hostname -I|cut -d. -f4`
datadir=${DATA_DIR}
socket=${DATA_DIR}/mysql.sock

[client]
socket=${DATA_DIR}/mysql.sock

[mysqld_safe]
log-error=${DATA_DIR}/mysql.log
pid-file=${DATA_DIR}/mysql.pid
EOF

			mysqld --initialize --user=mysql --datadir=${DATA_DIR}
			cp ${PREFIX}/support-files/mysql.server /etc/init.d/mysqld
			chmod +x /etc/init.d/mysqld
			chkconfig --add mysqld
			chkconfig mysqld on
			systemctl restart mysqld
			[ $? -ne 0 ] && { actions 'echo_error' "数据库启动失败，退出!" ; exit; }

			## 初始化数据库文件并提取 root 密码有以下两种方式, 这里我们使用方式1
			## 方式1 : 生成随机密码, 可以从 $DATA/mysql.log 中获取随机密码
			##		   	mysqld --initialize --user=mysql --datadir=/data/mysql
			##			修改口令, 口令是在初始化显示出来的, 所以需要自己手动输入 
			##				mysqladmin -uroot -p password NEW_PASS_WORD
			## 方式2 : 生成 root 空密码
			##			mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
			##			修改口令 mysqladmin -uroot   password NEW_PASS_WORD
			mysqladmin -uroot -p password $MYSQL_ROOT_PASSWORD &>/dev/null
			actions 'echo_success' "数据库安装完成"
			exit;
			;;
		## centos 7 二进制安装 MySQL 8.0.27 版本
		## 也需要进行安装加固的操作 mysql_secure_installation
		"centos_7_binary_install_mysql_8_0_27")
			SRC_DIR='/usr/local/src'
			MYSQL_VERSION='8.0.27'
			MYSQL_FILE="mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64.tar.xz"
			MYSQL_DIR="mysql-${MYSQL_VERSION}-linux-glibc2.12-x86_64"
			PREFIX="/apps/mysql-`echo ${MYSQL_VERSION} | tr . _`"
			DATA_DIR="/data/mysql-`echo ${MYSQL_VERSION} | tr . _`"
			MYSQL_DOWNLOAD_URL="https://mirrors.aliyun.com/mysql/MySQL-8.0/${MYSQL_FILE}"

			yum -y install libaio numactl-libs perl-Data-Dumper autoconf
			if [ $? -ne 0 ]; then
				actions 'echo_error' "编译工具安装失败, 请检查"
				exit;
			fi

			cd $SRC_DIR
			check
			mkdir -p $PREFIX ${DATA_DIR}
			tar xvf $MYSQL_FILE
			mv $MYSQL_DIR/* $PREFIX
			ln -sv $PREFIX /usr/local/mysql
			chown -R mysql.mysql $PREFIX
			chown -R mysql.mysql $DATA_DIR
			echo 'PATH='$PREFIX'/bin:$PATH' > /etc/profile.d/mysql.sh
			. /etc/profile.d/mysql.sh

			cat > /etc/my.cnf <<EOF
[mysqld]
server-id=`hostname -I|cut -d. -f4`
datadir=${DATA_DIR}
socket=${DATA_DIR}/mysql.sock

[client]
socket=${DATA_DIR}/mysql.sock

[mysqld_safe]
log-error=${DATA_DIR}/mysql.log
pid-file=${DATA_DIR}/mysql.pid
EOF

			mysqld --initialize --user=mysql --datadir=${DATA_DIR}
			cp ${PREFIX}/support-files/mysql.server /etc/init.d/mysqld
			chmod +x /etc/init.d/mysqld
			chkconfig --add mysqld
			chkconfig mysqld on
			systemctl start mysqld
			[ $? -ne 0 ] && { actions 'echo_error' "数据库启动失败，退出!" ; exit; }

			## 初始化数据库文件并提取 root 密码有以下两种方式, 这里我们使用方式1
			## 方式1 : 生成随机密码, 可以从 $DATA/mysql.log 中获取随机密码
			##		   	mysqld --initialize --user=mysql --datadir=/data/mysql
			##			修改口令, 口令是在初始化显示出来的, 所以需要自己手动输入 
			##				mysqladmin -uroot -p password NEW_PASS_WORD
			## 方式2 : 生成 root 空密码
			##			mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
			##			修改口令 mysqladmin -uroot   password NEW_PASS_WORD
			MYSQL_TEMPORARY_PASSWORD=`awk '/A temporary password/{print $NF}' ${DATA_DIR}/mysql.log`
			echo $MYSQL_TEMPORARY_PASSWORD;
			mysqladmin -uroot -p"$MYSQL_TEMPORARY_PASSWORD" password $MYSQL_ROOT_PASSWORD &>/dev/null
			actions 'echo_success' "数据库安装完成"
			exit;
			;;
		## centos 7 源码安装 MySQL 5.6.51 版本
		## 也需要进行安装加固的操作 mysql_secure_installation
		"centos_7_source_code_install_mysql_5_6_51")
			SRC_DIR='/usr/local/src'
			MYSQL_VERSION='5.6.51'
			MYSQL_FILE="mysql-${MYSQL_VERSION}.tar.gz"
			MYSQL_DIR="mysql-${MYSQL_VERSION}"
			PREFIX="/apps/mysql-`echo ${MYSQL_VERSION} | tr . _`"
			DATA_DIR="/data/mysql-`echo ${MYSQL_VERSION} | tr . _`"
			MYSQL_DOWNLOAD_URL="https://mirrors.aliyun.com/mysql/MySQL-5.6/${MYSQL_FILE}"

			yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel \
			libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel \
			libxml2-devel openssl-devel libevent-devel libaio libaio-devel perl-Data-Dumper autoconf
			
			if [ $? -ne 0 ]; then
				actions 'echo_error' "编译工具安装失败, 请检查"
				exit;
			fi

			cd $SRC_DIR
			check
			mkdir -p $PREFIX ${DATA_DIR}
			tar xvf $MYSQL_FILE
			chown -Rf root.root $MYSQL_DIR

			cd $MYSQL_DIR
			cmake . \
			-DCMAKE_INSTALL_PREFIX=${PREFIX} \
			-DMYSQL_DATADIR=${DATA_DIR} \
			-DSYSCONFDIR=/etc/ \
			-DMYSQL_USER=mysql \
			-DWITH_INNOBASE_STORAGE_ENGINE=1 \
			-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
			-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
			-DWITH_PARTITION_STORAGE_ENGINE=1 \
			-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
			-DWITH_DEBUG=0 \
			-DWITH_READLINE=1 \
			-DWITH_SSL=system \
			-DWITH_ZLIB=system \
			-DWITH_LIBWRAP=0 \
			-DENABLED_LOCAL_INFILE=1 \
			-DMYSQL_UNIX_ADDR=${DATA_DIR}/mysql.sock \
			-DDEFAULT_CHARSET=utf8 \
			-DDEFAULT_COLLATION=utf8_general_ci

			## 如果出错了, 则执行以下命令
			## 执行 rm -f CMakeCache.txt
			make -j 4 && make install	

			echo 'PATH='$PREFIX'/bin:$PATH' > /etc/profile.d/mysql.sh
			. /etc/profile.d/mysql.sh

			cd $PREFIX
			scripts/mysql_install_db --datadir=${DATA_DIR} --user=mysql

			chown -R mysql.mysql $PREFIX
			chown -R mysql.mysql $DATA_DIR

			# cp ${PREFIX}/support-files/my-default.cnf /etc/my.cnf

			cat > /etc/my.cnf <<EOF
[mysqld]
server-id=`hostname -I|cut -d. -f4`
datadir=${DATA_DIR}
socket=${DATA_DIR}/mysql.sock

[client]
socket=${DATA_DIR}/mysql.sock

[mysqld_safe]
log-error=${DATA_DIR}/mysql.log
pid-file=${DATA_DIR}/mysql.pid
EOF

			## 准备启动脚本,并启动服务
			cp ${PREFIX}/support-files/mysql.server /etc/init.d/mysqld
			chmod +x /etc/init.d/mysqld
			chkconfig --add mysqld
			chkconfig mysqld on
			systemctl start mysqld
			[ $? -ne 0 ] && { actions 'echo_error' "数据库启动失败，退出!" ; exit; }
			
			actions 'echo_success' "数据库安装完成"
			exit;
			;;
		## centos 7 源码安装 Mariadb 10.2.44 版本
		## 也需要进行安装加固的操作 mysql_secure_installation
		"centos_7_source_code_install_mariadb_10_2_44")
			SRC_DIR='/usr/local/src'
			MYSQL_VERSION='10.2.44'
			MYSQL_FILE="mariadb-${MYSQL_VERSION}.tar.gz"
			MYSQL_DIR="mariadb-${MYSQL_VERSION}"
			PREFIX="/apps/mariadb-`echo ${MYSQL_VERSION} | tr . _`"
			DATA_DIR="/data/mariadb-`echo ${MYSQL_VERSION} | tr . _`"
			MYSQL_DOWNLOAD_URL="https://mirrors.aliyun.com/mariadb/mariadb-10.2.44/source/${MYSQL_FILE}"

			yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel \
				libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel \
				openssl-devel libevent-devel libaio libaio-devel perl-Data-Dumper autoconf
			if [ $? -ne 0 ]; then
				actions 'echo_error' "编译工具安装失败, 请检查"
				exit;
			fi

			cd $SRC_DIR
			# check
			mkdir -p $PREFIX ${DATA_DIR}
			tar xvf $MYSQL_FILE
			chown -Rf root.root $MYSQL_DIR

			cd $MYSQL_DIR
			cmake . \
			-DCMAKE_INSTALL_PREFIX=${PREFIX} \
			-DMYSQL_DATADIR=${DATA_DIR} \
			-DSYSCONFDIR=/etc/ \
			-DMYSQL_USER=mysql \
			-DWITH_INNOBASE_STORAGE_ENGINE=1 \
			-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
			-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
			-DWITH_PARTITION_STORAGE_ENGINE=1 \
			-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
			-DWITH_DEBUG=0 \
			-DWITH_READLINE=1 \
			-DWITH_SSL=system \
			-DWITH_ZLIB=system \
			-DWITH_LIBWRAP=0 \
			-DENABLED_LOCAL_INFILE=1 \
			-DMYSQL_UNIX_ADDR=${DATA_DIR}/mysql.sock \
			-DDEFAULT_CHARSET=utf8 \
			-DDEFAULT_COLLATION=utf8_general_ci

			## 如果出错了, 则执行以下命令
			## 执行 rm -f CMakeCache.txt
			make -j 4 && make install	

			echo 'PATH='$PREFIX'/bin:$PATH' > /etc/profile.d/mysql.sh
			. /etc/profile.d/mysql.sh

			cd $PREFIX
			scripts/mysql_install_db --datadir=${DATA_DIR} --user=mysql

			chown -R mysql.mysql $PREFIX
			chown -R mysql.mysql $DATA_DIR

			# cp ${PREFIX}/support-files/my-huge.cnf /etc/my.cnf

			cat > /etc/my.cnf <<EOF
[mysqld]
server-id=`hostname -I|cut -d. -f4`
datadir=${DATA_DIR}
socket=${DATA_DIR}/mysql.sock

[client]
socket=${DATA_DIR}/mysql.sock

[mysqld_safe]
log-error=${DATA_DIR}/mysql.log
pid-file=${DATA_DIR}/mysql.pid
EOF

			## 准备启动脚本,并启动服务
			cp ${PREFIX}/support-files/mysql.server /etc/init.d/mysqld
			chmod +x /etc/init.d/mysqld
			chkconfig --add mysqld
			chkconfig mysqld on
			service mysqld start
			[ $? -ne 0 ] && { actions 'echo_error' "数据库启动失败，退出!" ; exit; }
			actions 'echo_success' "数据库安装完成"
			exit;
			;;				

		## 备份指定的单个数据库
		"centos_7_backup_one_database_mariadb_10_5")
			BACKUP_TIME=`date +%F_%H-%M-%S`
			BACKUP_DIR=/mysql_backup
			read -p "输入一个你要备份的数据库: " DB_NAME 
			actions 'echo_success' "开始备份"
			[ -d "$BACKUP_DIR" ] || mkdir $BACKUP_DIR
			mysqldump -F --single-transaction --flush-privileges --master-data=2 --default-character-set=utf8 -q -B $DB_NAME | gzip > ${BACKUP_DIR}/${DB_NAME}_${BACKUP_TIME}.sql.gz
			actions 'echo_success' "备份完成"
			exit;
			;;
		## centos 7 备份除了指定之外的所有数据库 mariadb 10.5 版本
		"centos_7_backup_all_database_mariadb_10_5")
			BACKUP_TIME=`date +%F_%H-%M-%S`
			BACKUP_DIR=/mysql_backup
			actions 'echo_success' "开始备份"
			[ -d "$BACKUP_DIR" ] || mkdir $BACKUP_DIR
			for DB in `mysql -uroot -p -e 'show databases' | grep -Ev "^Database|.*schema$"`; do
				mysqldump -F --single-transaction --flush-privileges --master-data=2 --default-character-set=utf8 -q -B $DB | gzip > ${BACKUP_DIR}/${DB}_${BACKUP_TIME}.sql.gz
			done
			actions 'echo_success' "备份完成"
			exit;
			;;
		*)
			echo "Selected number: $REPLY"
			echo "Selected number: $select_menu"
			exit;
			;;
	esac
done